package com.jeesite.modules.wzt.service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.jeesite.common.entity.Extend;
import com.jeesite.common.idgen.IdGen;
import com.jeesite.common.lang.StringUtils;
import com.jeesite.modules.sys.entity.Office;
import com.jeesite.modules.sys.service.OfficeService;
import com.jeesite.modules.wzt.util.BamboocloudUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.jeesite.common.entity.Page;
import com.jeesite.common.service.CrudService;
import com.jeesite.modules.wzt.entity.WztOrg;
import com.jeesite.modules.wzt.dao.WztOrgDao;

/**
 * 皖政通组织机构Service
 * @author xhc
 * @version 2023-03-20
 */
@Service
@Transactional(readOnly=false)
public class WztOrgService extends CrudService<WztOrgDao, WztOrg> {
	@Autowired
	private OfficeService officeService;
	/**
	 * 获取单条数据
	 * @param wztOrg
	 * @return
	 */
	@Override
	public WztOrg get(WztOrg wztOrg) {
		return super.get(wztOrg);
	}
	
	/**
	 * 查询分页数据
	 * @param wztOrg 查询条件
	 * @param  分页对象
	 * @return
	 */
	@Override
	public Page<WztOrg> findPage(WztOrg wztOrg) {
		return super.findPage(wztOrg);
	}
	
	/**
	 * 查询列表数据
	 * @param wztOrg
	 * @return
	 */
	@Override
	public List<WztOrg> findList(WztOrg wztOrg) {
		return super.findList(wztOrg);
	}
	
	/**
	 * 保存数据（插入或更新）
	 * @param wztOrg
	 */
	@Override
	@Transactional(readOnly=false)
	public void save(WztOrg wztOrg) {
		super.save(wztOrg);
	}
	
	/**
	 * 更新状态
	 * @param wztOrg
	 */
	@Override
	@Transactional(readOnly=false)
	public void updateStatus(WztOrg wztOrg) {
		super.updateStatus(wztOrg);
	}
	
	/**
	 * 删除数据
	 * @param wztOrg
	 */
	@Override
	@Transactional(readOnly=false)
	public void delete(WztOrg wztOrg) {
		super.delete(wztOrg);
	}
	@Transactional(readOnly=false)
	public Map<String, Object> createOrg(Map<String, Object> reqmap) {
		Map<String,Object> resMap = new HashMap<>();
		WztOrg wztOrg  = new WztOrg();
		try {
			BamboocloudUtils.copyProperties(reqmap,wztOrg);
		} catch (Exception e) {
			e.printStackTrace();
		}
		if(reqmap.get("code")==null){
			resMap.put("resultCode","500");
			resMap.put("message","机构 ID必传");
			return resMap;
		}
		if(reqmap.get("name")==null){
			resMap.put("resultCode","500");
			resMap.put("message","机构名称必传");
			return resMap;
		}
		if(reqmap.get("type")==null){
			resMap.put("resultCode","500");
			resMap.put("message","机构类型必传");
			return resMap;
		}
		if(StringUtils.isNotBlank(wztOrg.getParentId())){
			WztOrg  org  = dao.queryOfficeByWztOrgTable(wztOrg.getParentId());
			Office  office_db =dao.queryOfficeByExtends3(wztOrg.getParentId());
			if(org==null && office_db == null){
				resMap.put("resultCode","500");
				resMap.put("message","创建机构失败,父机构组织不存在请先创建");
				return resMap;
			}
			WztOrg  org_db  = dao.queryOfficeByWztOrgTable(wztOrg.getCode());
			//事先维护好的老数据关系表存在组织，就更新我们系统组织extend_s3
			wztOrg.setOfficeName(wztOrg.getName());
			if(org_db != null){
				Office office = officeService.get(org_db.getOfficeCode());
				office.setOfficeName(StringUtils.isBlank(office.getOfficeName())?wztOrg.getName():office.getOfficeName());
				Extend extend = new Extend();
				extend.setExtendS3(org_db.getCode());
				office.setExtend(extend);
				dao.update(wztOrg);
				officeService.save(office);
				resMap.put("uid",office.getOfficeCode());
				return resMap;
			}
			//如果不存在那就做新插入表wztorg,表office
			Office  office = new Office();
			office.setOfficeName(wztOrg.getName());
			office.setViewCode(IdGen.nextId());
			office.setParentCode(org.getOfficeCode());
			if(StringUtils.equals("GOV_INTERNAL_INSTITUTION",wztOrg.getType())){
				office.setOfficeType("1");
			}else {
				office.setOfficeType("0");
			}
			office.setFullName(wztOrg.getName());
			Extend extend = new Extend();
			extend.setExtendS3(wztOrg.getCode());
			office.setExtend(extend);
			officeService.save(office);
			wztOrg.setOfficeCode(office.getOfficeCode());
			dao.insert(wztOrg);
			resMap.put("uid",office.getOfficeCode());
			return resMap;
		}else{
			WztOrg  org_db  = dao.queryOfficeByWztOrgTable(wztOrg.getCode());
			Office  office = new Office();
			office.setOfficeName(wztOrg.getName());
			wztOrg.setOfficeName(wztOrg.getName());
			if(StringUtils.equals("GOV_INTERNAL_INSTITUTION",wztOrg.getType())){
				office.setOfficeType("1");
			}else {
				office.setOfficeType("0");
			}
			office.setFullName(wztOrg.getName());
			Extend extend = new Extend();
			extend.setExtendS3(wztOrg.getCode());
			office.setExtend(extend);
			if(org_db==null){
				office.setViewCode(IdGen.nextId());
				office.setParentCode("0");
				officeService.save(office);
				wztOrg.setOfficeCode(office.getOfficeCode());
				dao.insert(wztOrg);
			}else{
				office.setOfficeCode(org_db.getOfficeCode());
				dao.update(wztOrg);
				office.setOfficeName(null);
				officeService.save(office);
				wztOrg.setOfficeCode(org_db.getOfficeCode());
			}

			resMap.put("uid",wztOrg.getOfficeCode());
			return resMap;
		}

	}

	public WztOrg queryOfficeByWztOrgTable(String organizitionId) {
		return dao.queryOfficeByWztOrgTable(organizitionId);
	}
	@Transactional(readOnly=false)
	public Map<String, Object> updateOrg(Map<String, Object> reqmap) {

		Map<String,Object> resMap = new HashMap<>();
		if(reqmap.get("bimOrgId")==null){
			resMap.put("resultCode","500");
			resMap.put("message","机构bimOrgId必传");
			return resMap;
		}
		WztOrg wztOrg  = new WztOrg();
		wztOrg.setOfficeCode(reqmap.get("bimOrgId").toString());
		WztOrg wztOrg_db = dao.queryByofficeCode(reqmap.get("bimOrgId").toString());
		try {
			BamboocloudUtils.copyProperties(reqmap,wztOrg);
		} catch (Exception e) {
			e.printStackTrace();
		}

		if(wztOrg_db!=null) {
			wztOrg.setCode(wztOrg_db.getCode());
			Office  office = new Office();
			Office office_db = officeService.get(wztOrg.getOfficeCode());
			Office office_parent = dao.queryOfficeByExtends3(StringUtils.isBlank(wztOrg.getParentId())?wztOrg_db.getParentId():wztOrg.getParentId());
			office.setParentCode(office_db.getParentCode());
			if(office_parent!=null){
				office.setParentCode(office_parent.getOfficeCode());
			}
			wztOrg.setOfficeName(wztOrg_db.getName());
			dao.update(wztOrg);
			office.setOfficeName(wztOrg.getName());
			office.setOfficeCode(wztOrg.getOfficeCode());
			if (StringUtils.isNotBlank(wztOrg.getType())){
				if (StringUtils.equals("GOV_INTERNAL_INSTITUTION", wztOrg.getType())) {
					office.setOfficeType("1");
				} else {
					office.setOfficeType("0");
				}
		    }
			if(reqmap.get("__ENABLE__")!=null) {
				boolean status = (boolean)reqmap.get("__ENABLE__");
				if(status){
					office.setStatus("0");
				}else {
					office.setStatus("2");
				}
				dao.updateOfficeStatus(office);
			}
			office.setFullName(wztOrg.getName());
			Extend extend = new Extend();
			extend.setExtendS3(wztOrg.getCode());
			office.setExtend(extend);
			officeService.save(office);
		}
		return resMap;
	}
	@Transactional(readOnly=false)
	public Map<String, Object> deleteOrg(Map<String, Object> reqmap) throws Exception {
		Map<String,Object> resMap = new HashMap<>();
		if(reqmap.get("bimOrgId")==null){
			resMap.put("resultCode","500");
			resMap.put("message","机构bimOrgId必传");
			return resMap;
		}
		WztOrg wztOrg_db = dao.queryByofficeCode(reqmap.get("bimOrgId").toString());
		if(dao.delete(wztOrg_db)+dao.deleteOfficeByOfficeCode(wztOrg_db.getOfficeCode())!=2){
			throw new Exception("删除机构失败");
		}
		return resMap;
	}
}